<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--360浏览器优先以webkit内核解析-->
    <title>我的文章</title>
    <link rel="shortcut icon" href="assets/manager/favicon.ico"> 
    <link href="assets/manager/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="assets/manager/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="assets/manager/css/animate.css" rel="stylesheet">
    <link href="assets/manager/css/style.css?v=4.1.0" rel="stylesheet">
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div><h2 class="crumbs">我的文章</h2></div>
        <div class="row">
            <div class="col-sm-3">
                <span class="userMbq">标签：</span>
                <select id="tags" class="form-control m-b userMsleinp" name="account">
                </select>
            </div>
            <div class="col-sm-3">
                <span class="userMbq">标题：</span>
                <input type="text" id="title" class="form-control userMsleinp">
            </div>
            <div class="col-sm-1"><button type="button" id="select" class="btn btn-outline btn-info">搜索</button></div>
        </div>
        <div class="row" style="margin-bottom: 5px;">
            <a href="addarticle" type="button" class="btn btn-outline btn-info">添加文章</a>
            <button id="batchDelArticle" type="button" class="btn btn-outline btn-danger">批量删除</button>
        </div>
        <div class="row">
            <div class="ibox float-e-margins">
                <table class="table table-bordered usersTab">
                    <thead>
                        <tr>
                            <th width="3%">#</th>
                            <th width="59%">标题</th>
                            <th width="10%">标签</th>
                            <th width="10%">类型</th>
                            <th width="13%">发布时间</th>
                            <th width="15%">操作</th>
                        </tr>
                    </thead>
                    <tbody id="articleList">
                    </tbody>
                </table>
            </div>
            <div class="pageBox"><p></p></div>
        </div>
    </div>
    <!-- 全局js -->
    <script src="assets/manager/js/jquery.min.js?v=2.1.4"></script>
    <script src="assets/manager/js/bootstrap.min.js?v=3.3.6"></script>
    <script src="assets/manager/js/plugins/layer/layer.min.js"></script>
    <!-- Flot -->
    <script src="assets/manager/js/plugins/flot/jquery.flot.js"></script>
    <script src="assets/manager/js/plugins/flot/jquery.flot.tooltip.min.js"></script>
    <script src="assets/manager/js/plugins/flot/jquery.flot.resize.js"></script>
    <script src="assets/manager/js/plugins/flot/jquery.flot.pie.js"></script>
    <!-- 自定义js -->
    <script src="assets/manager/js/content.js"></script>
    <script src="assets/manager/js/pageBox.js"></script>
    <!--flotdemo-->
    <script type="text/javascript">
    $(document).ready(function(){
    	var $page=1;
    	var $pageSize=6;
    	var $tag = null;
    	var $title = null;
    	
    	$.get("getTags",function(data){
	    	$tagsBox = $("#tags");
	    	var $firstOption = $("<option>全部</option>");
	    	$tagsBox.append($firstOption);
			for(var i=0;i<data.length;i++){
				var $option = $("<option>" + data[i] + "</option>");
				$tagsBox.append($option);
			}
		});
    	
    	getArticles($page,$pageSize,$tag,$title);
    	function getArticles(rpage,rpageSise,rqtag,rqtitle){
    		$.post("getArticleByAuthor",
    			{page:rpage,pageSize:rpageSise,tag:rqtag,title:rqtitle},
    			function(data){
    			pageBox(data.pageInfo);
        		var $tbody = $("#articleList");
        		$("#articleList tr").remove();
        		for(var i=0;i<data.pageInfo.list.length;i++){
            		var $article = data.pageInfo.list[i];
            		var $id = $article.id;
            		var $postD = $article.postD.split(".")[0];
        			var $buttonId = $("<td><a href='editarticle?id=" + $article.id + "' type='button' data-id='" + $article.id + "' class='btn btn-outline btn-info edit'>编辑</a>" + 
                            "<a href='javascript:void(0);'type='button' data-id='" + $article.id + "' class='btn btn-outline btn-danger del'>删除</a></td>");
        			var $tr = $("<tr><td><input type='checkbox' value='" + $article.id + "'></td>"
        					+ "<td>" + $article.title + "</td>"
        					+ "<td>" + $article.tag + "</td>"
        					+ "<td>" + $article.type + "</td>"
        					+ "<td>" + $postD + "</td>"
        					+ "</tr>");
        			$tr.append($buttonId);
        			$tbody.append($tr);
        		}
        	});
    	}
    	//分页
        $(".pageBox").on("click","a",function(){
        	var $cPage = $(this).attr("data-page");
        	var $maxPage =  $(".pageBox p").find("a:last").html();
        	if($cPage  == 0 || $(this).attr("class") == "active"){
        		return false;
        	}else if($page == $maxPage && ($(this).html() == "下一页" || $(this).html() == "尾页")){
        		return false;
        	}else if($page == 1 && ($(this).html() == "上一页" || $(this).html() == "首页")){
        		return false;
        	}
        	else{
        		$page = $cPage;
        		getArticles($page,$pageSize,$tag,$title);
        		return false;
        	}
        });
      	//分页
    	
    	$("#articleList").on("click",".del",function(){
    		var $parentsTr = $(this).parents("tr");
    		var $parentsTrL = $(this).parents("tr").index();
    		var $del = confirm("确认要删除？");
    		if($del){
    			var $id = $(this).attr("data-id");
        		$.get("delArticle?id=" + $id,function(data){
        			if(data == 1){
        				alert("删除成功！");
        				$parentsTr.remove();
        				if($parentsTrL == 0){
        					$page = --$page;
        				}
        				getArticles($page,$pageSize,$tag,$title);
        			}
        		});
    		}
    	});
    	
    	$("#batchDelArticle").click(function(){
    		var $beforeTrL = $("#articleList tr").length;
    		var $del = confirm("确认要删除？");
    		if($del){
    			var $checkBoxs = $("#articleList input[type='checkbox']:checked");
    			
        		var $idArray = [];
        		var $checkBoxsL = $checkBoxs.length;
    	   		 if($checkBoxs.length > 0){
    	   			 for(var i=0;i<$checkBoxs.length;i++){
    	   				 $idArray[i] = $checkBoxs.eq(i).val();
    	   			 }
    	   			 $.get("batchDelArticle?list=" + $idArray,function(data){
    	   				 if(data == $checkBoxsL){
    	   					 alert("删除成功！");
    	   					 if($beforeTrL == $checkBoxsL && $page>1){
    	   						$page = --$page;
    	   					 }
    	   					getArticles($page,$pageSize,$tag,$title);
    	   				 }
    	   			 });
    	   		 }
    		}
    	});
    	
    	//搜索
    	$("#select").click(function(){
    		$tag = $("#tags").val();
    		if($tag == "全部"){
    			$tag=null;
    		}
    		$title = $("#title").val();
    		$page = 1;
    		$pageSize = 6;
    		getArticles($page,$pageSize,$tag,$title);
    	});
    	//搜索
    });
    </script>
</body>
</html>